# -*- coding: utf-8 -*-

import datetime, os

from program_top.utilities.my_datetime import my_date_time_without_micro

class my_logger(object):
	'''记录日志的实例'''
	
	def __init__(self, log_writting_instance,ignore_id=False):
		'''如果哪个类想要写入日志，可以把自己传入日志类的构造函数'''
		
		self.__logged_id=id(log_writting_instance)#被记录类实例的id
		self.__logged_class=log_writting_instance.__class__.__name__#被记录实例的类名称
		
		self.logger_name="%s,%d"%(self.__logged_class, self.__logged_id) if (not ignore_id) else "%s"%(self.__logged_class)
		log_path=log_writting_instance.environment_pack['runtime_paths']['output_dir']
		self.__log_file_encoding=log_writting_instance.environment_pack['current_platform_info'][
			'current_system_encoding']#读取当前运行操作系统的内码，输出日志文件时使用
		log_filename=log_path+self.logger_name+'.log'
		self.__log_filename=log_filename
		
		log_begin_moment_this_time=datetime.datetime.now()
		log_start_string="本次日志开始时间,%s"%log_begin_moment_this_time.strftime(my_date_time_without_micro)
		self.write_log(log_start_string)
	
	def write_log(self, log_message):
		time_now=datetime.datetime.now().strftime(my_date_time_without_micro)
		join_message='……'.join([time_now, log_message])
		try:
			join_message=join_message.decode('utf-8').encode(self.__log_file_encoding)
		except Exception as current_error:
			
			pass
		
		print(join_message)
		
		with open(self.__log_filename, mode='a',newline='',encoding='utf-8') as file_struct:
			file_struct.write(log_message+'\r\n')
			file_struct.close()
			pass
		pass
	pass